package com.yl.applicationsecurity.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yl.applicationsecurity.mapper.*;
import com.yl.applicationsecurity.pojo.*;
import com.yl.applicationsecurity.service.SgmOtherVulnerabilityService;
import com.yl.applicationsecurity.service.TodoListService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.DayOfWeek;
import java.time.temporal.TemporalAdjusters;

import java.util.*;

/**
 * @author 鹤仙问鹿仙
 * @program: Application-security
 * @description:
 * @date 2025-02-19 17:28:23
 */
@Service
public class TodoListServiceImpl   implements TodoListService {

    @Autowired
    private InspectionMapper inspectionMapper;


    @Autowired
    private InspectionConfigurationMapper InspectionConfigurationMapper;


    @Autowired
    private ComponentUnauthorizedMapper componentUnauthorizedMapper;

    @Autowired
    private RadarVulnerabilityMapper radarVulnerabilityMapper;

    @Autowired
    private AssetsMapper assetsMapper;

    @Autowired
    private SgmSafeRiskMapper sgmSafeRiskMapper;


    @Autowired
    private SgmOtherVulnerabilityMapper sgmOtherVulnerabilityMapper;

    @Autowired
    private KunpengSqlInjectionMapper kunpengSqlInjectionMapper;
    @Autowired
    private ScaVulnerabilityMapper scaVulnerabilityMapper;


    @Override
    public Map getData(String[] nationSelect) {

        Map<String,Long> map = new HashMap<String,Long>();
        //获取登录入口巡检相关的数据-待处理缺陷数量
        LambdaQueryWrapper<Inspection> inspectionLambdaQueryWrapper = new LambdaQueryWrapper<>();

        if ( nationSelect!=null&nationSelect.length!=0){
            inspectionLambdaQueryWrapper.in(Inspection::getNation,nationSelect);
        }
        inspectionLambdaQueryWrapper.eq(Inspection::getStatus, 0);
        Long   inspectionToBeProcessedCount = inspectionMapper.selectCount(inspectionLambdaQueryWrapper);
        map.put("inspectionToBeProcessedCount", inspectionToBeProcessedCount);
        //获取登录入口巡检相关的数据-告警总数量
        inspectionLambdaQueryWrapper = new LambdaQueryWrapper<>();

        if ( nationSelect!=null&nationSelect.length!=0){
            inspectionLambdaQueryWrapper.in(Inspection::getNation,nationSelect);
        }
        Long   inspectionCount = inspectionMapper.selectCount(inspectionLambdaQueryWrapper);
        map.put("inspectionCount", inspectionCount);

        //获取登录入口巡检相关的数据-监测接口梳理
        LambdaQueryWrapper<InspectionConfiguration> InspectionConfigurationLambdaQueryWrapper = new LambdaQueryWrapper<>();

        if ( nationSelect!=null&nationSelect.length!=0){
            InspectionConfigurationLambdaQueryWrapper.in(InspectionConfiguration::getNation,nationSelect);
        }


        Long   inspectionConfigurationCount = InspectionConfigurationMapper.selectCount(InspectionConfigurationLambdaQueryWrapper);
        map.put("inspectionConfigurationCount", inspectionConfigurationCount);



        //获取组件未授权扫描相关数据-待处理告警数量
        LambdaQueryWrapper<ComponentUnauthorized> componentUnauthorizedQueryWrapper = new LambdaQueryWrapper<>();

        if ( nationSelect!=null&nationSelect.length!=0){
            componentUnauthorizedQueryWrapper.in(ComponentUnauthorized::getNation,nationSelect);
        }
        componentUnauthorizedQueryWrapper.eq(ComponentUnauthorized::getStatus, 0);
        Long   componentUnauthorizedToBeProcessedCount = componentUnauthorizedMapper.selectCount(componentUnauthorizedQueryWrapper);
        map.put("componentUnauthorizedToBeProcessedCount", componentUnauthorizedToBeProcessedCount);


        //获取组件未授权扫描相关数据缺陷总数量
         componentUnauthorizedQueryWrapper = new LambdaQueryWrapper<>();

        if ( nationSelect!=null&nationSelect.length!=0){
            componentUnauthorizedQueryWrapper.in(ComponentUnauthorized::getNation,nationSelect);
        }
        Long   componentUnauthorizedCount = componentUnauthorizedMapper.selectCount(componentUnauthorizedQueryWrapper);
        map.put("componentUnauthorizedCount", componentUnauthorizedCount);



        // 安全雷达--待处理漏洞数量
        LambdaQueryWrapper<RadarVulnerability> radarVulnerabilityQueryWrapper = new LambdaQueryWrapper<>();

        if ( nationSelect!=null&nationSelect.length!=0){
            radarVulnerabilityQueryWrapper.in(RadarVulnerability::getNation,nationSelect);
        }
        radarVulnerabilityQueryWrapper.eq(RadarVulnerability::getStatus, 0);
        Long   radarVulnerabilityToBeProcessedCount = radarVulnerabilityMapper.selectCount(radarVulnerabilityQueryWrapper);
        map.put("radarVulnerabilityToBeProcessedCount", radarVulnerabilityToBeProcessedCount);

        //安全雷达--漏洞总数量
        radarVulnerabilityQueryWrapper = new LambdaQueryWrapper<>();

        if ( nationSelect!=null&nationSelect.length!=0){
            radarVulnerabilityQueryWrapper.in(RadarVulnerability::getNation,nationSelect);
        }
        Long   radarVulnerabilityCount = radarVulnerabilityMapper.selectCount(radarVulnerabilityQueryWrapper);
        map.put("radarVulnerabilityCount", radarVulnerabilityCount);

        //安全雷达--发现资产数量
        LambdaQueryWrapper<Assets> assetsQueryWrapper = new LambdaQueryWrapper<>();

        if ( nationSelect!=null&nationSelect.length!=0){
            assetsQueryWrapper.in(Assets::getNation,nationSelect);
        }


        Long   assetsCount = assetsMapper.selectCount(assetsQueryWrapper);
        map.put("assetsCount", assetsCount);


        //交互式逻辑探针--待处理告警数量
        LambdaQueryWrapper<SgmSafeRisk> sgmSafeRiskQueryWrapper = new LambdaQueryWrapper<>();

        if ( nationSelect!=null&nationSelect.length!=0){
            sgmSafeRiskQueryWrapper.in(SgmSafeRisk::getCountry,nationSelect);
        }
        sgmSafeRiskQueryWrapper.eq(SgmSafeRisk::getStatus, 0);
        Long   sgmSafeRiskToBeProcessedCount = sgmSafeRiskMapper.selectCount(sgmSafeRiskQueryWrapper);
        map.put("sgmSafeRiskToBeProcessedCount", sgmSafeRiskToBeProcessedCount);

        //交互式逻辑探针--告警总数量
        sgmSafeRiskQueryWrapper = new LambdaQueryWrapper<>();

        if ( nationSelect!=null&nationSelect.length!=0){
            sgmSafeRiskQueryWrapper.in(SgmSafeRisk::getCountry,nationSelect);
        }
        Long   sgmSafeRiskCount = sgmSafeRiskMapper.selectCount(sgmSafeRiskQueryWrapper);
        map.put("sgmSafeRiskCount", sgmSafeRiskCount);
        //交互式逻辑探针--本周新增告警数量
        // 获取今天的日期
        LocalDate today = LocalDate.now();

        // 计算本周一的日期（周一作为一周的开始）
        LocalDate startOfWeek = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));

        // 计算本周的结束日期（周日作为一周的结束）
        LocalDate endOfWeek = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));

        sgmSafeRiskQueryWrapper = new LambdaQueryWrapper<>();
        if ( nationSelect!=null&nationSelect.length!=0){
            sgmSafeRiskQueryWrapper.in(SgmSafeRisk::getCountry,nationSelect);
        }
        sgmSafeRiskQueryWrapper.between(SgmSafeRisk::getFirstTime, startOfWeek, endOfWeek);
        Long   sgmSafeRiskNewSaveCount = sgmSafeRiskMapper.selectCount(sgmSafeRiskQueryWrapper);
        map.put("sgmSafeRiskNewSaveCount", sgmSafeRiskNewSaveCount);

        //交互式逻辑探针--超出漏洞处置时效告警数量
            //主要查询两种情况之和，一种是非待处理状态的告警，最近一次人工处理时间<超期时间,第二种是状态为待处理状态的告警，超期时间大于当前时间，这里只查询出第一种就可以
        sgmSafeRiskQueryWrapper = new LambdaQueryWrapper<>();
        sgmSafeRiskQueryWrapper.eq(SgmSafeRisk::getStatus, 0);
        sgmSafeRiskQueryWrapper.lt(SgmSafeRisk::getOverdueDate, new Date());
        if ( nationSelect!=null&nationSelect.length!=0){
            sgmSafeRiskQueryWrapper.in(SgmSafeRisk::getCountry,nationSelect);
        }
        Long   sgmSafeRiskoverdueDateCount = sgmSafeRiskMapper.selectCount(sgmSafeRiskQueryWrapper);
        map.put("sgmSafeRiskoverdueDateCount", sgmSafeRiskoverdueDateCount);

//        sgmSafeRiskQueryWrapper = new LambdaQueryWrapper<>();
//        sgmSafeRiskQueryWrapper.ne(SgmSafeRisk::getStatus, 0);
//        sgmSafeRiskQueryWrapper.apply("last_disposal_date > overdue_date");
//        if ( nationSelect!=null&nationSelect.length!=0){
//            sgmSafeRiskQueryWrapper.in(SgmSafeRisk::getCountry,nationSelect);
//        }
//       sgmSafeRiskoverdueDateCount = sgmSafeRiskoverdueDateCount+sgmSafeRiskMapper.selectCount(sgmSafeRiskQueryWrapper);
//        map.put("sgmSafeRiskoverdueDateCount", sgmSafeRiskoverdueDateCount);


        //交互式逻辑探针-其他漏洞-本周新增告警数量
        LambdaQueryWrapper<SgmOtherVulnerability> SgmOtherVulnerabilityQueryWrapper = new LambdaQueryWrapper<>();

        if ( nationSelect!=null&nationSelect.length!=0){
            SgmOtherVulnerabilityQueryWrapper.in(SgmOtherVulnerability::getCountry,nationSelect);
        }
        Long   sgmOtherVulnerabilityCount = sgmOtherVulnerabilityMapper.selectCount(SgmOtherVulnerabilityQueryWrapper);
        map.put("sgmOtherVulnerabilityCount", sgmOtherVulnerabilityCount);
        //交互式逻辑探针--待处理告警数量
        SgmOtherVulnerabilityQueryWrapper.eq(SgmOtherVulnerability::getStatus, 0);
        Long sgmOtherVulnerabilityToBeProcessedCount =  sgmOtherVulnerabilityMapper.selectCount(SgmOtherVulnerabilityQueryWrapper);
        map.put("sgmOtherVulnerabilityToBeProcessedCount", sgmOtherVulnerabilityToBeProcessedCount);



        // 鲲鹏SQL注入监控--待处理漏洞数量
        LambdaQueryWrapper<KunpengSqlInjection> kunpengSqlInjectionQueryWrapper = new LambdaQueryWrapper<>();

        if ( nationSelect!=null&nationSelect.length!=0){
            kunpengSqlInjectionQueryWrapper.in(KunpengSqlInjection::getNation,nationSelect);
        }
        kunpengSqlInjectionQueryWrapper.eq(KunpengSqlInjection::getStatus, 0);
        Long  kunpengSqlInjectionToBeProcessedCount = kunpengSqlInjectionMapper.selectCount(kunpengSqlInjectionQueryWrapper);
        map.put("kunpengSqlInjectionToBeProcessedCount", kunpengSqlInjectionToBeProcessedCount);

        //鲲鹏SQL注入监控--漏洞总数量
        kunpengSqlInjectionQueryWrapper = new LambdaQueryWrapper<>();

        if ( nationSelect!=null&nationSelect.length!=0){
            kunpengSqlInjectionQueryWrapper.in(KunpengSqlInjection::getNation,nationSelect);
        }
        Long   kunpengSqlInjectionCount = kunpengSqlInjectionMapper.selectCount(kunpengSqlInjectionQueryWrapper);
        map.put("kunpengSqlInjectionCount", kunpengSqlInjectionCount);


        // SCA开源组件管控--待处理漏洞数量
        LambdaQueryWrapper<ScaVulnerability> scaVulnerabilityQueryWrapper = new LambdaQueryWrapper<>();

        if ( nationSelect!=null&nationSelect.length!=0){
            scaVulnerabilityQueryWrapper.in(ScaVulnerability::getNation,nationSelect);
        }
        scaVulnerabilityQueryWrapper.eq(ScaVulnerability::getStatus, 0);
        Long scaVulnerabilityToBeProcessedCount = scaVulnerabilityMapper.selectCount(scaVulnerabilityQueryWrapper);
        map.put("scaVulnerabilityToBeProcessedCount", scaVulnerabilityToBeProcessedCount);

        //SCA开源组件管控--漏洞总数量
        scaVulnerabilityQueryWrapper = new LambdaQueryWrapper<>();

        if ( nationSelect!=null&nationSelect.length!=0){
            scaVulnerabilityQueryWrapper.in(ScaVulnerability::getNation,nationSelect);
        }
        Long   scaVulnerabilityCount = scaVulnerabilityMapper.selectCount(scaVulnerabilityQueryWrapper);
        map.put("scaVulnerabilityCount", scaVulnerabilityCount);




        return map;
    }

    @Override
    public List<Map> getExportDate() {

        //定义每个人负责的国家作为基础查询条件
        HashMap<String, String> basicsMap = new HashMap<>();
        basicsMap.put("永森 CN|JT|YL", "CN|JT|YL");
        basicsMap.put("永幸 YE|YC|JTM|UPFOS|STS|LDP|JY", "YE|YC|JTM|UPFOS|STS|LDP|JY");
        basicsMap.put("慧敏 SG|MY|CB", "SG|MY|CB");
        basicsMap.put("伟东 TH|VN|PH", "TH|VN|PH");
        basicsMap.put("永泽 SA|MX|BR|EG", "SA|MX|BR|EG");
        basicsMap.put("冯琨 IDN", "IDN");



        List<Map> list = new ArrayList<>();
        //查询出永森负责国家 CN|JT|YL
        for(String key:basicsMap.keySet()){
            HashMap<String, Object> map = new HashMap<>();
            map.put("name",key);
            String s = basicsMap.get(key);
            String[] nationSelect = s.split("\\|");
            //待处理交互逻辑探针-越权告警
            LambdaQueryWrapper<SgmSafeRisk> sgmSafeRiskQueryWrapper = new LambdaQueryWrapper<>();
            sgmSafeRiskQueryWrapper.in(SgmSafeRisk::getCountry,nationSelect);
            sgmSafeRiskQueryWrapper.eq(SgmSafeRisk::getStatus, 0);
            Long   sgmSafeRiskToBeProcessedCount = sgmSafeRiskMapper.selectCount(sgmSafeRiskQueryWrapper);
            map.put("sgmSafeRiskToBeProcessedCount", sgmSafeRiskToBeProcessedCount);
            //待处理交互逻辑探针-其他告警
            LambdaQueryWrapper<SgmOtherVulnerability> SgmOtherVulnerabilityQueryWrapper = new LambdaQueryWrapper<>();
            SgmOtherVulnerabilityQueryWrapper.in(SgmOtherVulnerability::getCountry,nationSelect);
            SgmOtherVulnerabilityQueryWrapper.eq(SgmOtherVulnerability::getStatus, 0);
            Long   sgmOtherVulnerabilityCount = sgmOtherVulnerabilityMapper.selectCount(SgmOtherVulnerabilityQueryWrapper);
            map.put("sgmOtherVulnerabilityToBeProcessedCount", sgmOtherVulnerabilityCount);

            //待处理登录入口巡检告警
            LambdaQueryWrapper<Inspection> inspectionLambdaQueryWrapper = new LambdaQueryWrapper<>();

            if ( nationSelect!=null&nationSelect.length!=0){
                inspectionLambdaQueryWrapper.in(Inspection::getNation,nationSelect);
            }
            inspectionLambdaQueryWrapper.eq(Inspection::getStatus, 0);
            Long   inspectionToBeProcessedCount = inspectionMapper.selectCount(inspectionLambdaQueryWrapper);
            map.put("inspectionToBeProcessedCount", inspectionToBeProcessedCount);

            //待处理组件未授权扫描告警
            LambdaQueryWrapper<ComponentUnauthorized> componentUnauthorizedQueryWrapper = new LambdaQueryWrapper<>();
            componentUnauthorizedQueryWrapper.in(ComponentUnauthorized::getNation,nationSelect);
            componentUnauthorizedQueryWrapper.eq(ComponentUnauthorized::getStatus, 0);
            Long   componentUnauthorizedToBeProcessedCount = componentUnauthorizedMapper.selectCount(componentUnauthorizedQueryWrapper);
            map.put("componentUnauthorizedToBeProcessedCount", componentUnauthorizedToBeProcessedCount);


            //待处理安全雷达告警
            LambdaQueryWrapper<RadarVulnerability> radarVulnerabilityQueryWrapper = new LambdaQueryWrapper<>();
            radarVulnerabilityQueryWrapper.in(RadarVulnerability::getNation,nationSelect);
            radarVulnerabilityQueryWrapper.eq(RadarVulnerability::getStatus, 0);
            Long   radarVulnerabilityToBeProcessedCount = radarVulnerabilityMapper.selectCount(radarVulnerabilityQueryWrapper);
            map.put("radarVulnerabilityToBeProcessedCount", radarVulnerabilityToBeProcessedCount);
            list.add(map);

        }
        return list;
    }
}

